
var YehunSocket = function (param) {
    var webSocket = null;
    var options = {
        url: null,
        onOpen:    function(e) { console.log(e); },
        onMessage: function(e) { console.log(e); },
        onError:   function(e) { console.log(e); },
        onClose:   function(e) { console.log(e);
            if (webSocket !== null) {
                webSocket.close();
                webSocket = null;
            }
        },
        send: function (text) {
            if (webSocket !== null) {
                webSocket.send(text);
            }
        }
    };
    $.extend(true, options, param);
    if (options.url === null || options.url === '') {
        throw "WebSocket url is null";
    }
    if ('WebSocket' in window) {
        webSocket = new WebSocket(options.url);
    } else if ('MozWebSocket' in window) {
        webSocket = new MozWebSocket(url);
    } else {
        throw "WebSocket initialize failed!";
    }
    webSocket.onopen    = function(e) { options.onOpen(e);    };
    webSocket.onclose   = function(e) { options.onClose(e);   };
    webSocket.onerror   = function(e) { options.onError(e);   };
    webSocket.onmessage = function(e) { options.onMessage(e); };
    return webSocket;
};